package com.neuedu.studentmanage.controller;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.studentmanage.entity.Pager;
import com.neuedu.studentmanage.entity.Result;
import com.neuedu.studentmanage.entity.Subject;
import com.neuedu.studentmanage.service.SubjectService;

@Controller
public class SubjectController {
	
	 @Autowired 
	 private SubjectService subjectService;
	
	@RequestMapping("subject/index")
	public String sunjectIndex() {
		System.out.println(1);
		return "admin/subject";
	}
	
	@PostMapping("Subject/list")
	@ResponseBody
	public Pager<Subject> subjectList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "5") Integer rows) {
		Pager<Subject> pager = new Pager<>(page, rows);
		List<Subject> subjectList = subjectService.findListByPager(pager);
		Integer total = subjectService.findTotalByPager(pager);
		pager.setRows(subjectList);
		pager.setTotal(total);// 把total存进来
		
		return pager;
	}
	
	@RequestMapping("subject/add")
	@ResponseBody
	public Result doAdd(Subject subject) {
		Result result=subjectService.add(subject);
		return result;
	}
	
	@RequestMapping("subject/edit")
	@ResponseBody
	public Result doEdit(Subject subject) {
		Result result=subjectService.edit(subject);
		return result;
	}
	
	@RequestMapping("subject/delete")
	@ResponseBody
	//admin接收传参
	public Result doDelete(Integer subId) {
		
		List<Subject> list=new ArrayList<Subject>();
		list.add(new Subject(subId, null, null,null));
		
		Result result=subjectService.deleteByIds(list);
		return result;
	}
	

}
